package net.csdn.business.discuss.api.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.csdn.business.discuss.api.mapper.DiscussVoteMapper;
import net.csdn.business.discuss.api.mapper.DiscussVoteOptionMapper;
import net.csdn.business.discuss.api.mapper.DiscussVoteRecordMapper;
import net.csdn.business.discuss.api.model.dto.DiscussVoteDTO;
import net.csdn.business.discuss.api.model.dto.NewDiscussVoteDTO;
import net.csdn.business.discuss.api.model.dto.UpdateDiscussVoteDTO;
import net.csdn.business.discuss.api.model.entity.Discuss;
import net.csdn.business.discuss.api.model.entity.DiscussVote;
import net.csdn.business.discuss.api.model.entity.DiscussVoteRecord;
import net.csdn.business.discuss.api.model.query.DiscussVoteQuery;
import net.csdn.business.discuss.api.model.vo.DiscussVoteVO;
import net.csdn.business.discuss.api.service.IDiscussVoteService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 讨论投票信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-25
 */
@Service
public class DiscussVoteServiceImpl extends ServiceImpl<DiscussVoteMapper, DiscussVote> implements IDiscussVoteService
{

    @Autowired
    private DiscussVoteOptionMapper discussVoteOptionMapper;

    @Autowired
    private DiscussVoteRecordMapper discussVoteRecordMapper;

    /**
     * 分页查询讨论投票信息列表
     *
     * @param query 讨论投票信息
     * @return 讨论投票信息
     */
    @Override
    public IPage<DiscussVoteVO> findPageList(DiscussVoteQuery query) {
        //获得Page对象
        Page<DiscussVoteVO> page = query.getPage();
        List<DiscussVoteVO> pageList=baseMapper.findPageList(page,query);
        page.setRecords(pageList);
        return page;
    }

    /**
     * 根据id查询讨论投票信息
     * 
     * @param id 讨论投票信息主键
     * @return 讨论投票信息
     */
    @Override
    public DiscussVoteVO selectVoteByDiscussId(String id)
    {
        return baseMapper.selectVoteByDiscussId(id);
    }

    /**
     * 查询讨论投票信息列表
     * 
     * @param query 讨论投票信息
     * @return 讨论投票信息
     */
    @Override
    public List<DiscussVoteVO> selectDiscussVoteList(DiscussVoteQuery query)
    {
        return baseMapper.selectDiscussVoteList(query);
    }

    /**
     * 新增讨论投票信息
     *
     * @param params 讨论投票信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertDiscussVote(NewDiscussVoteDTO params)
    {
        DiscussVote discussVote=new DiscussVote();
        BeanUtils.copyProperties(params,discussVote);
        discussVote.setCreatedBy(params.getCurrUserId());
        String id= IdUtil.fastSimpleUUID();
        discussVote.setObjectId(id);
        baseMapper.insert(discussVote);
        return id;
    }

    /**
     * 修改讨论投票信息
     *
     * @param updateDiscussVoteDTO 讨论投票信息
     * @return 结果
     */
    @Override
    public Integer updateDiscussVote(UpdateDiscussVoteDTO updateDiscussVoteDTO)
    {
        DiscussVote discussVote=new DiscussVote();
        BeanUtils.copyProperties(updateDiscussVoteDTO,discussVote);
        return baseMapper.updateDiscussVote(discussVote);
    }


    /**
     * 根据讨论id,删除讨论投票信息
     * 
     * @param discussId 讨论投票信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteVoteByDiscussId(String discussId)
    {
        int result=baseMapper.deleteVoteByDiscussId(discussId);
        discussVoteOptionMapper.deleteVoteOptionByDiscussId(discussId);
        discussVoteRecordMapper.deleteVoteRecordByDiscussId(discussId);
        return result;
    }
}
